package Backend

import (
	"bytes"
	"crypto/cipher"
	"crypto/des"
	"fmt"
	"hello/controllers/Base"

	"reflect"
)

type TestController struct {
	Base.BaseController

}

func (this *TestController) Prepare() {
	fmt.Println("每次都执行")
}


func (this *TestController) Index() {
	//redsync.Test()
	//list:=redsync.UnqID()
	//fmt.Println(len(list))


	this.AjaxSuccess(1231)
}

func (this *TestController) Upload() {
	callback:= this.GetPost("CKEditorFuncNum");
	//ckCsrfToken
	//
	path:="http://qiniu.wlphp.cn/static/upload/2019/06/02/0f761354f581d87c12cb2a5001f9d234.jpg"


	str:="<script type='text/javascript'>%s</script>"

	js:="window.parent.CKEDITOR.tools.callFunction('%s','%s','')"
	//	"window.parent.CKEDITOR.tools.callFunction('"+callback+"',/img/uploadImg/','')"

	//fmt.Printf("asdasd %d",123123)

	s:=fmt.Sprintf(js,callback,path)

	s=fmt.Sprintf(str,s)


	fmt.Printf(str)

	this.Ctx.WriteString(s)

	//this.AjaxSuccess(s)
}

func typeof(v interface{}) string {
	return reflect.TypeOf(v).String()
}

func DesEncrypt(origData, key []byte) ([]byte, error) {
	block, err := des.NewCipher(key)
	if err != nil {
		return nil, err
	}
	origData = PKCS5Padding(origData, block.BlockSize())
	blockMode := cipher.NewCBCEncrypter(block, key)
	crypted := make([]byte, len(origData))
	blockMode.CryptBlocks(crypted, origData)
	return crypted, nil
}

func PKCS5Padding(cipherText []byte, blockSize int) []byte {
	padding := blockSize - len(cipherText) % blockSize
	padText := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(cipherText, padText...)
}

func DesDecrypt(crypted, key []byte) ([]byte, error) {
	block, err := des.NewCipher(key)
	if err != nil {
		return nil, err
	}
	blockMode := cipher.NewCBCDecrypter(block, key)
	origData := make([]byte, len(crypted))
	// origData := crypted
	blockMode.CryptBlocks(origData, crypted)
	origData = PKCS5UnPadding(origData)
	// origData = ZeroUnPadding(origData)
	return origData, nil
}


func PKCS5UnPadding(origData []byte) []byte {
	length := len(origData)
	// 去掉最后一个字节 unpadding 次
	unpadding := int(origData[length-1])
	return origData[:(length - unpadding)]
}




